/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.game.entitysystem.systems;

import java.util.HashMap;
import com.jme3.scene.Node;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public class PhysicsTransformSystem_Server extends PhysicsTransformSystem{
    
    public PhysicsTransformSystem_Server(Node rootNode, int playerEntityID){
        super(rootNode, playerEntityID);
    }
    private HashMap<Integer, Long> lastModificationTimestamps_Location = new HashMap<Integer, Long>();
    private HashMap<Integer, Long> lastModificationTimestamps_Rotation = new HashMap<Integer, Long>();
    
    @Override
    protected boolean isApplyingTransformAllowed_Location(Entity entity){
        return (entity.getComponent(LocationComponent.class).getTimestamp() > getLastModificationTimestamp(lastModificationTimestamps_Location, entity.getID()));
    }
    
    @Override
    protected boolean isApplyingTransformAllowed_Rotation(Entity entity){
        return (entity.getComponent(RotationComponent.class).getTimestamp() > getLastModificationTimestamp(lastModificationTimestamps_Rotation, entity.getID()));
    }
    
    private static long getLastModificationTimestamp(HashMap<Integer, Long> lastModificationTimestamps, int entityID){
        if(lastModificationTimestamps.containsKey(entityID)){
            return lastModificationTimestamps.get(entityID);
        }
        return -1;
    }

    @Override
    protected void onTransformApplied_Location(Entity entity){
        super.onTransformApplied_Location(entity);
        lastModificationTimestamps_Location.put(entity.getID(), entity.getComponent(LocationComponent.class).getTimestamp());
    }

    @Override
    protected void onTransformApplied_Rotation(Entity entity){
        super.onTransformApplied_Rotation(entity);
        lastModificationTimestamps_Rotation.put(entity.getID(), entity.getComponent(RotationComponent.class).getTimestamp());
    }
}
